package com.such.kit.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import com.such.kit.string.StringCommons;

/**
 * @author SUCH
 * <pre>
 * 异常处理通用工具类
 * </pre>
 */
public class ExceptionCommons {

	/**
	 * <pre>
	 * 获取异常堆栈字符串
	 * </pre>
	 * @param e 异常
	 * @return
	 */
	public static String getStackTrace(Throwable e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		return sw.toString();
	}

	/**
	 * <pre>
	 * 获取异常堆栈字符串
	 * </pre>
	 * @param e 异常
	 * @param length 窃取字符串长度
	 * @return
	 */
	public static String getStackTrace(Throwable e, int length) {
		return StringCommons.substring(getStackTrace(e), 0, length, true);
	}

}
